package com.digitalchina.lxnsy.api;

import android.app.Activity;
import android.os.Handler;

import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.xml.UserXML;
import com.lzy.okhttputils.model.HttpParams;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.api
 * 创建人：白彦东
 * 创建时间：二〇一七年一月十日 10:38:43
 * 修改备注：
 * ================================================
 */
public class BusinessOrders {

    /**
     * 拆单并生成订单
     *
     * @param activity
     * @param orderVOList 订单 列表json格式
     * @param handler
     */
    public static void generateOrders(Activity activity, String orderVOList, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("buyerId", UserXML.getUserId(activity));
        params.put("buyerName", UserXML.getLoginName(activity)); //买家姓名
        params.put("buyerLogo", UserXML.getImagePath(activity));// 买家头像
        params.put("consignee", UserXML.getLoginName(activity)); //  收货人
        params.put("shippingAddress", "默认收货地址"); // 收货地址
        params.put("shippingPhone", UserXML.getMobilePhone(activity)); // 联系电话
        params.put("tip", ""); // 买家备注
        params.put("paymentType", "1"); // 支付类型 0-线下支付，1-在线支付
        params.put("orderVOList", orderVOList);
        params.put("appChannel", Constant.APP_CHANNEL);

        OkHttpUtil.post(activity, URL.URL_GENERATE_ORDERS, "", params, handler,
                MSG.MSG_GENERATE_ORDERS_SUCCESS, MSG.MSG_GENERATE_ORDERS_FIELD);

    }

    /**
     * 判断商品是否过期
     *
     * @param activity
     * @param @As(",") List<String> productIds 商品id集合
     * @param handler
     */
    public static void isProductsExpired(Activity activity, String productIds, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("productIds", productIds);

        OkHttpUtil.post(activity, URL.URL_IS_PRODUCTS_EXPIRED, "", params, handler,
                MSG.MSG_IS_PRODUCTS_EXPIRED_SUCCESS, MSG.MSG_IS_PRODUCTS_EXPIRED_FIELD);

    }

    /**
     * 获取订单详情
     *
     * @param activity
     * @param orderNo  订单号
     * @param handler
     */
    public static void getOrderView(Activity activity, String orderNo, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("buyerId", UserXML.getUserId(activity));
        params.put("orderNo", orderNo);

        OkHttpUtil.post(activity, URL.URL_GET_ORDER_DETAILS, "", params, handler,
                MSG.MSG_GET_ORDER_DETAILS_SUCCESS, MSG.MSG_GET_ORDER_DETAILS_FIELD);
    }

    /**
     * 支付流水单
     *
     * @param activity
     * @param paymentMethod 支付方式。"0":支付宝 ；"1"：微信
     * @param orderNo       订单号
     * @param notifyUrl     在notify接口中，回调应用的地址，应用用于做个性化业务处理
     * @param handler
     */
    public static void paymentJournal(Activity activity, String paymentMethod, String orderNo, String notifyUrl, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("paymentMethod", paymentMethod);
        params.put("orderNo", orderNo);
        params.put("notifyUrl", notifyUrl);
//        params.put("returnUrl", ""); //在return接口接口中，回调应用的页面。移动端支付可不传递参数
        params.put("appChannel", Constant.APP_CHANNEL);

        OkHttpUtil.post(activity, URL.URL_PAYMENT_JOURNAL, "", params, handler,
                MSG.MSG_PAYMENT_JOURNAL_SUCCESS, MSG.MSG_PAYMENT_JOURNAL_FIELD);
    }

    /**
     * 微信签名
     *
     * @param activity
     * @param orderNo     订单号
     * @param paymentBody 商品简单描述。比如杨凌农业大数据中心
     * @param notifyUrl   在notify接口中，回调应用的地址，应用用于做个性化业务处理
     * @param handler
     */
    public static void unifiedOrder(Activity activity, String orderNo, String paymentBody, String notifyUrl, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("orderNo", orderNo);
        params.put("paymentBody", paymentBody);
        params.put("notifyUrl", notifyUrl);
        params.put("appChannel", Constant.APP_CHANNEL);

        OkHttpUtil.post(activity, URL.URL_WX_PAY_SIGN, "", params, handler,
                MSG.MSG_WX_PAY_SIGN_SUCCESS, MSG.MSG_WX_PAY_SIGN_FIELD);
    }

    /**
     * 支付宝签名
     *
     * @param activity
     * @param orderNo   订单号
     * @param notifyUrl 在notify接口中，回调应用的地址，应用用于做个性化业务处理
     * @param handler
     */
    public static void instantAccount(Activity activity, String orderNo, String notifyUrl, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("orderNo", orderNo);
        params.put("notifyUrl", notifyUrl);
        params.put("callBackUrl", ""); //在return接口接口中，回调应用的页面。移动端支付可不传递参数
        params.put("appChannel", Constant.APP_CHANNEL);

        OkHttpUtil.post(activity, URL.URL_ALI_PAY_SIGN, "", params, handler,
                MSG.MSG_ALI_PAY_SIGN_SUCCESS, MSG.MSG_ALI_PAY_SIGN_FIELD);
    }


    /**
     * 获取用户订单列表
     *
     * @param activity //     * @param shippingType 发货类型：0-不需发货 1-自提 2-邮寄  目前暂时不传
     * @param page
     * @param limit
     * @param handler
     */
    public static void getOrderList(Activity activity, String page, String limit, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("buyerId", UserXML.getUserId(activity));
        params.put("shippingType", "2");
        params.put("page", page);
        params.put("limit", limit);

        OkHttpUtil.post(activity, URL.URL_GET_ORDER_LIST, "", params, handler,
                MSG.MSG_GET_ORDER_LIST_SUCCESS, MSG.MSG_GET_ORDER_LIST_FIELD);
    }


    /**
     * 根据状态获取订单列表
     *
     * @param activity
     * @param queryStatus 22 - "待评价"
     *                    23 - "已关闭" (包含 24 - "已取消" 和 "已评价")
     *                    29 - "待支付"
     * @param page
     * @param limit
     * @param handler
     */
    public static void getOrderByStatus(Activity activity, String queryStatus, String page, String limit, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("buyerId", UserXML.getUserId(activity));
        params.put("shippingType", "2");
        params.put("queryStatus", queryStatus);
        params.put("page", page);
        params.put("limit", limit);

        OkHttpUtil.post(activity, URL.URL_GET_ORDER_BY_STATUS, "", params, handler,
                MSG.MSG_GET_ORDER_BY_STATUS_SUCCESS, MSG.MSG_GET_ORDER_BY_STATUS_FIELD);
    }

    /**
     * 买家取消订单
     *
     * @param activity
     * @param orderNo      订单号
     * @param cancelReason 取消原因
     * @param handler
     */
    public static void cancelOrder(Activity activity, String orderNo, String cancelReason, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("buyerId", UserXML.getUserId(activity));
        params.put("orderNo", orderNo);
        params.put("cancelReason", cancelReason);

        OkHttpUtil.post(activity, URL.URL_CANCEL_ORDER, "请稍候...", params, handler, MSG.MSG_CANCEL_ORDER_SUCCESS, MSG.MSG_CANCEL_ORDER_FIELD);

    }

    /**
     * 删除订单
     *
     * @param activity
     * @param orderNo  订单号
     * @param handler
     */
    public static void deleteOrder(Activity activity, String orderNo, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("buyerId", UserXML.getUserId(activity));
        params.put("orderNo", orderNo);

        OkHttpUtil.post(activity, URL.URL_DELETE_ORDER, "请稍候...", params, handler, MSG.MSG_DELETE_ORDER_SUCCESS, MSG.MSG_DELETE_ORDER_FIELD);

    }
}
